package admin

import (
	"context"
	"github.com/gogf/gf/v2/util/gconv"
	"miaoyu/internal/dao"
	"miaoyu/internal/model"
	"miaoyu/internal/model/admin"
	"miaoyu/internal/model/entity"
	"miaoyu/internal/service"
	function "miaoyu/utility"
	"time"
)

type sLogin struct {
}

func init() {
	service.RegisterLogin(NewLogin())
}

func NewLogin() *sLogin {
	return &sLogin{}
}

func (s *sLogin) CheckUserPassword(ctx context.Context, input admin.UserLoginInput) (resUser entity.MyUser, resErr error) {
	//对密码进行rsa解密
	originData, rsaErr := function.RsaDecrypt([]byte(input.Password))
	if rsaErr != nil {
		resErr = rsaErr
		return
	}
	var shaWord string = function.SHA1String(gconv.String(originData))

	resUser, resErr = dao.MyUser.GetUserByUserNamePassword(ctx, input.UserName, shaWord)

	if resErr != nil {
		return
	}
	return
}
func (s *sLogin) DoLogin(ctx context.Context, user entity.MyUser) (string, time.Time, error) {
	payloadMap := s.getPayloadFormat(user)
	token, expire, err := service.Auth().TokenGenerator(payloadMap)
	return token, expire, err
}
func (s *sLogin) getPayloadFormat(data entity.MyUser) (res model.ContextUser) {

	res.Id = gconv.Uint(data.Id)
	res.UserName = data.UserName
	return
}
